<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>promise</h1>
<hr>

<script>
    let getJSON = function (url) {
        return new Promise(function (resolve, reject) {
            let xhr = new XMLHttpRequest();
            xhr.open('GET', url);
            xhr.onreadystatechange = function () {
                if (xhr.readyState !== 4) {
                    return;
                }

                if (xhr.status === 200) {
                    resolve(xhr.response);
                } else {
                    reject(new Error(xhr.statusText));
                }
            }
            xhr.send();
        });
    }

    getJSON("http://api.icndb.com/jokes/random")
        .then(function (responseText) {
            return JSON.parse(responseText);
        })
        .then(function (obj) {
            console.log(obj.value.joke);
        })
        .catch(function (err) {
            console.log(err.message);
        });
</script>
</body>
</html>